import React from 'react';
import { Card, Table } from 'antd';
// import { AdvancedSearch } from '@/components/UserForm';
import AdvancedSearch from '@/components/AdvancedSearch';
import './DataTable.less';
import Pagination from '@/components/Pagination';
import useTable from './useTable';

const DataTable = ({
    filters,
    sorters,
    columns,
    children,
    rowKey,
    cardProps,
    data,
    pagination,
    handleTable,
    loading,
    form,
    formConfig = {},
    collapsed,
    setCollapsed,
    handleSubmit,
    tableProps
}) => {
    const { baseForm = null } = formConfig;
    if (children && typeof children !== 'function') {
        throw new Error('DataTable 组件必须接受一个函数作为子节点！');
    }
    const table = (
        <>
            <Table
                className="vv-dataTable-table"
                columns={columns}
                dataSource={data}
                loading={loading}
                onChange={handleTable}
                rowKey={rowKey}
                pagination={false}
                {...tableProps}
            />
            <Pagination {...pagination} onChange={handleTable} filters={filters} sorter={sorters} loading={loading} />
        </>
    );
    const query = (
        <AdvancedSearch
            form={form}
            onSubmit={handleSubmit}
            onReset={handleSubmit}
            colConfig={{
                xxl: 8,
                xl: 12,
                lg: 12,
                md: 24,
                sm: 24,
                xs: 24
            }}
        >
            {baseForm.props.children}
        </AdvancedSearch>
    );
    if (children) {
        // @ts-ignore
        return children({
            query,
            table,
            handleSubmit,
            collapsed,
            setCollapsed
        });
    }
    return (
        <Card className="vv-dataTable" bordered={false} {...cardProps}>
            {query}
            {table}
        </Card>
    );
};

DataTable.defaultProps = {
    updateQuery: () => {},
    defaultCollapsed: true
};

DataTable.useTable = useTable;

export default DataTable;
